#!/usr/bin/env python

 

import argparse
import sys


parser = argparse.ArgumentParser(
    description='''对重叠的gff 进行精简
    用法:
    simple_gff.py -i denovo_target.gff3 -o denovo_target_simple.gff3
    ''')


parser.add_argument('-i',
                help='输入')

parser.add_argument('-o',
                help='输出')

args = parser.parse_args()

if not args.i or not args.o:
    parser.print_help()
    sys.exit()

# 对重叠的gff 进行精简

infile = args.i

outfile = args.o

outfile = open(outfile,'w')

gene_lista = []


t = 0



with open(infile) as fila:
	for i in fila:
		k = i.strip().split('#')[0].split('\t')
		if len(k)>5:
			if k[2] =='gene':
				scaf = k[0]
				start = int(k[3])
				end = int(k[4])
				direct = k[6]
				t = 1
				for l in gene_lista:
					if l[3] == direct and l[0]==scaf:
						target_start = l[1]
						target_end = l[2]
						if (start >= target_start and start <= target_end) or (end >= target_start and end <= target_end) or (start <= target_start and end >= target_end):
							t = 0
							break
				if t==1:
					gene_lista.append([scaf,start,end,direct])
					outfile.write('\t'.join(k)+'\n')
			else:
				if t == 1:
					outfile.write('\t'.join(k)+'\n')

